﻿// Copyright (c) 2007-2021 西安交通信息投资营运有限公司 版权所有
// 作者:吕艳阳

namespace XCI.Core
{
    /// <summary>
    /// 分页信息
    /// </summary>
    public class PageInfo
    {
        private int _recordEndIndex;
        private int _recordStartIndex;

        /// <summary>
        /// 构造分页信息
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="total">总记录数</param>
        public PageInfo(int pageIndex, int pageSize, int total)
        {
            if (pageSize <= 0) pageSize = 20;
            PageSize = pageSize;
            if (total < 0) total = 0;
            Total = total;
            TotalPage = (Total + PageSize - 1) / PageSize;
            if (pageIndex > TotalPage) pageIndex = TotalPage;
            if (pageIndex < 1) pageIndex = 1;
            PageIndex = pageIndex;
        }

        /// <summary>
        /// 构造分页信息
        /// </summary>
        /// <param name="pageIndex">页码索引,从1开始</param>
        /// <param name="pageSize">每页记录数</param>
        public PageInfo(int pageIndex, int pageSize)
        {
            if (pageSize <= 0) pageSize = 20;
            if (pageIndex < 1) pageIndex = 1;
            PageSize = pageSize;
            PageIndex = pageIndex;
        }

        /// <summary>
        /// 修复页码值,返回正确的页码值.
        /// <para>页码不能小于1</para>
        /// <para>根据总记录数total和每页大小pageSize计算总页数,如果页码超过总页码则强制把页码改为总页码数</para>
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="total">总记录数</param>
        /// <returns>返回正确的页码值</returns>
        public static int RepairPageIndex(int pageIndex, int pageSize, int total)
        {
            if (pageSize <= 0) pageSize = 20;
            if (total < 0) total = 0;
            var totalPage = (total + pageSize - 1) / pageSize;
            if (pageIndex > totalPage) pageIndex = totalPage;
            if (pageIndex < 1) pageIndex = 1;
            return pageIndex;
        }

        /// <summary>
        /// 页码索引
        /// </summary>
        public int PageIndex { get; private set; }

        /// <summary>
        /// 每页大小
        /// </summary>
        public int PageSize { get; private set; }

        /// <summary>
        /// 记录总数
        /// </summary>
        public int Total { get; }

        /// <summary>
        /// 总页数
        /// </summary>
        public int TotalPage { get; private set; }

        /// <summary>
        /// 当前记录开始数
        /// </summary>
        public int StartIndex
        {
            get
            {
                if (Total > 0)
                {
                    _recordStartIndex = (PageIndex - 1) * PageSize + 1;
                }

                return _recordStartIndex;
            }
        }

        /// <summary>
        /// 当前记录结束数
        /// </summary>
        public int EndIndex
        {
            get
            {
                var start = StartIndex;
                _recordEndIndex = start + PageSize - 1;
                if (_recordEndIndex > Total)
                {
                    _recordEndIndex = Total;
                }

                return _recordEndIndex;
            }
        }

        /// <summary>
        /// 是否首页
        /// </summary>
        public bool IsFirstPage => (PageIndex == 1);

        /// <summary>
        /// 是否有上一页
        /// </summary>
        public bool HasPreviousPage => (PageIndex > 1);

        /// <summary>
        /// 是否有下一页
        /// </summary>
        public bool HasNextPage => (PageIndex < TotalPage);

        /// <summary>
        /// 是否最后一页
        /// </summary>
        public bool IsLastPage => (PageIndex == TotalPage);

        /// <summary>
        /// 是否有数据
        /// </summary>
        public bool Has => Total > 0;
    }
}